package cn.wp.ewallet;

import cn.hutool.core.util.ArrayUtil;
import cn.wp.common.core.Wp;
import cn.wp.common.core.WpSetting;
import cn.wp.common.core.startup.StartupContainer;
import cn.wp.common.core.web.interceptor.WpInterceptorRegistryProxy;
import cn.wp.common.core.web.service.ServiceConfigurations;
import cn.wp.ewallet.upms.web.interceptor.RepeatLoginInterceptor;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;

import java.util.TimeZone;

/**
 * 项目配置类
 * Created by chenyb on 2019/09/23
 */
@Configuration
public class EwalletConfiguration {

    /**
     * 序列化时区问题
     * @return
     */
    @Bean
    @Profile({"test","prod"})  // 测试与生产环境下生效
    public Jackson2ObjectMapperBuilderCustomizer jacksonObjectMapperCustomization() {
        return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder.timeZone(TimeZone.getDefault());
    }

    @Bean
    public WpSetting ewalletSetting(){
        return new WpSetting() {

            // 添加cors跨域支持
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedMethods("*")
                        .allowedOrigins("*")
                        .allowedHeaders("*");
                super.addCorsMappings(registry);
            }

            @Override
            public void addViewControllers(ViewControllerRegistry registry) {

            }

            @Override
            public void configService(ServiceConfigurations serviceConfigurations) {
                //ServiceConfiguration serviceConfiguration = new ServiceConfiguration("demoServiceImpl");
                //serviceConfigurations.register(serviceConfiguration);
            }

            @Override
            public void configStartup(StartupContainer startupConfigurations) {
                startupConfigurations.addStartup(new EwalletSchedulerStartup());
            }

            @Override
            public void configInterceptor(WpInterceptorRegistryProxy interceptorRegistryProxy) {
                String[] excludes = new String[]{"/**/find*","/**/login","/**/logout"};
                String[] ignores = ArrayUtil.addAll(interceptorRegistryProxy.getIgnores(),excludes);
                //interceptorRegistryProxy.addInterceptor(new DemoModeInterceptor(interceptorRegistryProxy.getPathPatterns(),ignores));
                //interceptorRegistryProxy.removeInterceptor();
                interceptorRegistryProxy.addInterceptor(new RepeatLoginInterceptor(interceptorRegistryProxy.getPathPatterns(),ignores));
            }

            @Override
            public void onStart(Wp wp) {
                System.err.println("Ewallet Start Finish");
            }
        };
    }


}
